// i.r(t), i.d(t, { default: () => InteractionModeModule })
import Module from "./Module"
import SymbolList from "../SymbolList"
import { PointerButtonEvent } from "../event/PointerEvent"
import { KeyEvent } from "../event/KeyEvent"
import InteractionmodeData from "../data/InteractionmodeData"
import { InteractionSourceChangeMessage, InteractionModeChangeMessage } from "../message/inter.message"
import browserUtil from "../utils/browser.util"
import interactiveEnum, { deviceList } from "../enum/interactive.enum"
import pointDeviceEnum from "../enum/pointDevice.enum"
export default class InteractionModeModule extends Module {
  onPointerButtonEvent: (e: any) => void
  data: any
  renderer: any
  onKeyEvent: () => void
  engine: any
  mobileBrowser: boolean
  constructor() {
    super(...arguments),
      (this.name = "interactionmode"),
      (this.onPointerButtonEvent = e => {
        let t = this.data.source
        switch (e.device) {
          case pointDeviceEnum.TOUCH:
            t = interactiveEnum.Touch
            break
          case pointDeviceEnum.MOUSE:
            t = interactiveEnum.Mouse
            break
          case pointDeviceEnum.PEN:
            t = interactiveEnum.Pen
            break
          case pointDeviceEnum.GAMEPAD:
            this.renderer.xr.enabled && this.renderer.xr.isPresenting && (t = interactiveEnum.XRController)
            break
          default:
            this.log.debug("source:", e.device, e)
        }
        this.updateSource(t)
      }),
      (this.onKeyEvent = () => {
        this.updateSource(interactiveEnum.Key)
      })
  }
  async init(e, t) {
    ;(this.engine = t), (this.data = new InteractionmodeData()), (this.mobileBrowser = browserUtil.isMobile())
    const i = await t.getModuleBySymbol(SymbolList.WEBGL_RENDERER)
    ;(this.renderer = i.threeRenderer),
      this.updateMode(this.getInteractionMode(), this.data.mode),
      this.engine.market.register(this, InteractionmodeData, this.data),
      t.getModuleBySymbol(SymbolList.INPUT).then(e => {
        this.bindings.push(e.registerHandler(PointerButtonEvent, this.onPointerButtonEvent), e.registerHandler(KeyEvent, this.onKeyEvent))
      })
  }
  onUpdate(e) {
    const t = this.getInteractionMode()
    this.updateMode(t, this.data.mode)
  }
  getInteractionMode() {
    if (this.renderer.xr.enabled && this.renderer.xr.isPresenting) {
      const e = this.renderer.xr.getSession()
      if (null !== e && e.inputSources.length > 0)
        switch (e.inputSources[0].targetRayMode) {
          case "gaze":
          case "screen":
            return deviceList.VrOrientOnly
          case "tracked-pointer":
            return deviceList.VrWithTrackedController
        }
      return deviceList.VrOrientOnly
    }
    return this.mobileBrowser ? deviceList.Mobile : deviceList.Desktop
  }
  updateMode(e, t) {
    e !== this.data.mode && (this.data.updateMode(e), this.data.commit(), this.engine.broadcast(new InteractionModeChangeMessage(this.data.mode, t)))
  }
  updateSource(e) {
    e !== this.data.source && (this.data.updateSource(e), this.data.commit(), this.engine.broadcast(new InteractionSourceChangeMessage(this.data.source)))
  }
}
